//
//  BNTabBarController.m
//  BNCodeGuide
//
//  Created by kevin007 on 2020/10/9.
//  Copyright © 2020 kevin007. All rights reserved.
//

#import "BNTabBarController.h"

#import "BNHomeController.h"
#import "BNMineController.h"

@interface BNTabBarController ()

@end

@implementation BNTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
     
    [self setupUI];
}

#pragma mark - setupUI
- (void)setupUI{
    self.tabBar.translucent = YES;
    self.tabBar.tintColor = [UIColor blueColor];
    [UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
    [UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blueColor]} forState:UIControlStateSelected];
    
    
    NSArray *items = @[@{@"class" : @"BNHomeController",
                         @"title" : @"首页",
                         @"image" : @"tabBar_home_normal",
                         @"selectedImage" : @"tabBar_home_selected"},
                       @{@"class" : @"BNMineController",
                         @"title" : @"我的",
                         @"image" : @"tabBar_mine_normal",
                         @"selectedImage" : @"tabBar_mine_selected"}];
       

    [self setupChildControllers:items];
    
}

- (void)setupChildControllers:(NSArray *)items
{
    NSMutableArray *tmp = [NSMutableArray array];
    for (NSDictionary *item in items) {
        NSString *title = item[@"title"];
        NSString *className = item[@"class"];
        UIImage *image = [UIImage imageNamed:item[@"image"]];
        UIImage *selectedImage = [UIImage imageNamed:item[@"selectedImage"]];
        
        UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image selectedImage:selectedImage];
        tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -2);
    
        Class vcClass = NSClassFromString(className);
        UIViewController *vc = [[vcClass alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        nav.tabBarItem = tabBarItem;
        [tmp addObject:nav];
    }
    
    self.viewControllers = tmp;
}

@end
